/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import com.google.code.sapien.dao.CategoryAclDAO;
import com.google.code.sapien.dao.CategoryDAO;
import com.google.code.sapien.dao.RoleDAO;
import com.google.code.sapien.model.Category;
import com.google.code.sapien.model.CategoryAcl;
import com.google.code.sapien.model.Role;
import com.google.code.sapien.model.User;
import com.google.inject.Inject;

/**
 * Default implementation of the {@link com.google.code.sapien.service.CategoryAclService}.
 * @author Adam
 * @version $Id: CategoryAclSapienService.java 25 2009-05-26 04:23:01Z a.ruggles $
 * 
 * Created on Mar 17, 2009 at 11:24:37 PM 
 */
public class CategoryAclSapienService implements CategoryAclService {
	/**
	 * The category ACL data access object.
	 */
	private final CategoryAclDAO categoryAclDAO;

	/**
	 * The category data access object.
	 */
	private final CategoryDAO categoryDAO;

	/**
	 * The role data access object.
	 */
	private final RoleDAO roleDAO;

	/**
	 * Constructs a category ACL sapien service.
	 * @param categoryAclDAO The category ACL data access object.
	 */
	@Inject
	public CategoryAclSapienService(final CategoryAclDAO categoryAclDAO, final CategoryDAO categoryDAO,
			final RoleDAO roleDAO) {
		this.categoryAclDAO = categoryAclDAO;
		this.categoryDAO = categoryDAO;
		this.roleDAO = roleDAO;
		
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#add(java.lang.Object,
	 * com.google.code.sapien.security.SecurityDetails)
	 */
	public void add(final CategoryAcl entity, final User creator) {
		entity.setRole(roleDAO.load(entity.getRole().getId()));
		entity.setCategory(categoryDAO.load(entity.getCategory().getId()));
		categoryAclDAO.add(entity);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#get(java.io.Serializable)
	 */
	public CategoryAcl get(final Serializable id) {
		return categoryAclDAO.get(id);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryAclService#list(com.google.code.sapien.model.Category)
	 */
	public List<CategoryAcl> list(final Category category) {
		return categoryAclDAO.list(category);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryAclService#list(java.util.Collection)
	 */
	public List<CategoryAcl> list(final Collection<Role> roles) {
		return categoryAclDAO.list(roles);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryAclService#list(com.google.code.sapien.model.Role)
	 */
	public List<CategoryAcl> list(final Role role) {
		return categoryAclDAO.list(role);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryAclService#listForGuests()
	 */
	public List<CategoryAcl> listForGuests() {
		return categoryAclDAO.listForGuests();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.CategoryAclService#listForNewUsers()
	 */
	public List<CategoryAcl> listForNewUsers() {
		return categoryAclDAO.listForNewUsers();
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#remove(java.lang.Object,
	 * com.google.code.sapien.security.SecurityDetails)
	 */
	public void remove(final CategoryAcl entity, final User remover) {
		final CategoryAcl categoryAcl = categoryAclDAO.load(entity.getId());
		categoryAclDAO.remove(categoryAcl);
	}

	/**
	 * {@inheritDoc}
	 * @see com.google.code.sapien.service.EntityService#update(java.lang.Object,
	 * com.google.code.sapien.security.SecurityDetails)
	 */
	public CategoryAcl update(final CategoryAcl entity, final User updator) {
		final CategoryAcl categoryAcl = categoryAclDAO.load(entity.getId());
		categoryAcl.getPermissions().set(entity.getPermissions());
		return categoryAcl;
	}

}
